package com.ziyun.erp.web.modules.suppOffline.controller;

import java.util.Arrays;
import java.util.Map;

import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.suppOffline.dto.SupplierAuditDto;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.suppOffline.entity.ErpSupplierOfflineEntity;
import com.ziyun.erp.modules.suppOffline.service.ErpSupplierOfflineService;
import io.renren.common.utils.R;

/**
 * 线下外协商
 *
 * @author gaosheng
 * @email 
 * @date 2018-09-21 13:07:53
 */
@RestController
@RequestMapping("suppOffline/erpsupplieroffline")
public class ErpSupplierOfflineController {

    @Autowired
    private ErpSupplierOfflineService erpSupplierOfflineService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("suppOffline:erpsupplieroffline:list")
    public R list(@RequestParam Map<String, Object> params){
        return erpSupplierOfflineService.queryPageErpSupplierList(params);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{supplierId}")
    @RequiresPermissions("suppOffline:erpsupplieroffline:info")
    public R info(@PathVariable("supplierId") Integer supplierId){
        return erpSupplierOfflineService.info(supplierId);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("suppOffline:erpsupplieroffline:save")
    public R save(@RequestBody ErpSupplierOfflineEntity erpSupplierOffline){
        ValidatorUtils.validateEntity(erpSupplierOffline);
        Long userId = UserUtils.getCurentUser().getUserId();
        erpSupplierOfflineService.saveInfo(erpSupplierOffline, userId);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("suppOffline:erpsupplieroffline:update")
    public R update(@RequestBody ErpSupplierOfflineEntity erpSupplierOffline){
        ValidatorUtils.validateEntity(erpSupplierOffline);
        Long userId = UserUtils.getCurentUser().getUserId();
        erpSupplierOfflineService.updateInfo(erpSupplierOffline, userId);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("suppOffline:erpsupplieroffline:delete")
    public R delete(@RequestBody Integer[] supplierIds){
        Long userId = UserUtils.getCurentUser().getUserId();
        erpSupplierOfflineService.deleteInfoById(Arrays.asList(supplierIds), userId);

        return R.ok();
    }

    /**
     * 外协审核
     * @param supplierAuditDto
     * @return
     */
    @RequestMapping("/audit")
    public R audit(@RequestBody SupplierAuditDto supplierAuditDto){
        // 审核人
        Long userId = UserUtils.getCurentUser().getUserId();
        supplierAuditDto.setAuditName(userId.toString());
        return erpSupplierOfflineService.audit(supplierAuditDto);
    }

}
